﻿package com.kiklink.model.service;

import java.net.SocketTimeoutException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.kiklink.model.constants.ErrorCodeConstants;
import com.kiklink.model.constants.SharePreferencesConstants;
import com.kiklink.model.domain.MyProduct;
import com.kiklink.model.domain.Order;
import com.kiklink.model.domain.Page;
import com.kiklink.model.domain.Product;
import com.kiklink.model.domain.Shop;
import com.kiklink.model.domain.User;
import com.kiklink.model.util.DesEncryption;
import com.kiklink.model.util.HttpUtil;
import com.kiklink.model.util.JsonToObject;
import com.kiklink.model.util.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @author luang
 *
 */
public class KiklinkModel {

	private static KiklinkModel uniqueInstance = null;

	private static String sessionId = null;

	private static Context mcontext;

	private static final String SERVER_URL = "http://www.kiklink.com:80/";

	private KiklinkModel() {
	}

	public static KiklinkModel getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new KiklinkModel();

		}
		return uniqueInstance;
	}

	public static void initialize(Context context) {
		mcontext = context;
	}

	public boolean isKiklinkLoggedIn() {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(mcontext);
		return sp.getBoolean(SharePreferencesConstants.PREF_LOGIN_STATE, false);

	}
	private void handleHttpFailure(Throwable throwable, GetObjectCallback callback) {
		// TODO 自动生成的方法存根
		

		if(throwable instanceof SocketTimeoutException)
		{
			callback.onFailed(
					ErrorCodeConstants.TIME_OUT,
					ErrorCodeConstants.TIME_OUT_WORD);
		}
		else {
			callback.onFailed(
					ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
					ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
		}
	}
	private void handleHttpFailure(Throwable throwable,
			GetResultCallback callback) {
		// TODO 自动生成的方法存根
		if(throwable instanceof SocketTimeoutException)
		{
			callback.onFailed(
					ErrorCodeConstants.TIME_OUT,
					ErrorCodeConstants.TIME_OUT_WORD);
		}
		else {
			callback.onFailed(
					ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
					ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
		}
	}

	public void getAdProductPage(final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsBiaodanDetail/listAjax.do?inputListId=20&ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
				
			}



			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				Page page = JsonToObject.JsonToPage(arg0, "AdProduct");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	/**
	 * @param pageno
	 * @param keyword
	 * @param sort
	 *            buyedCount desc:人气排序、price：价格、distance:距离排序
	 * @param cat
	 *            10球类/20健身瑜伽/30运动恢复/40水上冰上/50极限户外/60舞蹈/70射击搏击格斗
	 * @param pricefrom
	 *            价格底限
	 * @param priceto
	 *            价格上限
	 * @param mapx
	 *            地图经度
	 * @param mapy
	 *            地图维度
	 * @param callback
	 * 
	 *            pricefrom,priceto,mapx,mapy 均为double类型，缺省时请传入-1
	 * 
	 * 
	 *            传入mapx,mapy，且sort参数等于distance时，按距离排序，距离字段为ext10
	 */
	public void getAllProductPage(final int pageno, final String keyword,
			final String sort, final String cat, final double pricefrom,
			final double priceto, final double mapx, final double mapy,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerProduct/search.do?status=1&ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数

		params.put("sessionId", sessionId);
		params.put("pageno", pageno);
		params.put("name", keyword);
		params.put("sort", sort);
		params.put("cat", cat);
		
		if (mapx != -1) {
			params.put("mapx", mapx);
		}
		if (mapy != -1) {
			params.put("mapy", mapy);
		}
		if (pricefrom != -1) {
			params.put("pricefrom", pricefrom);
		}
		if (priceto != -1) {
			params.put("priceto", priceto);
		}

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Product");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	/**
	 * @param pageno
	 * @param keyword
	 * @param sort
	 * @param cat
	 *            10球类/20健身瑜伽/30运动恢复/40水上冰上/50极限户外/60舞蹈/70射击搏击格斗
	 * @param callback
	 */
	public void getProductPage(final int pageno, final String keyword,
			final String sort, final String cat,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerProduct/search.do?status=1&ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("pageno", pageno);
		params.put("name", keyword);
		params.put("sort", sort);
		params.put("cat", cat);
		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Product");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	public void getProduct(final int id, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerProduct/show.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("id", id);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("product");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Product product = JsonToObject.JsonToProduct(jObject);
				if (product == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(product);

			};
		});

	}

	public void getCommentPage(final int pageno, final int id,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerProduct/productcomment.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("id", id);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("comments");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Comment");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});

	}

	public void getCoachPage(final int id, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerProduct/coaches.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("id", id);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				Page page = JsonToObject.JsonToPage(arg0, "Coach");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	public void getShop(final int shopId, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlSellerShop/show.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("id", shopId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("shop");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Shop shop = JsonToObject.JsonToShop(jObject);
				if (shop == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(shop);

			};
		});

	}

	public void login(final String username, final String password,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/login.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("moboremail", username);
		params.put("passwd", password);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				} else {

					JSONObject jObject = arg0.optJSONObject("user");
					if (jObject == null) {
						callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
								ErrorCodeConstants.NO_RESULT_FILL_WORD);
						return;
					}
					User user = JsonToObject.JsonToUser(jObject);
					if (user == null) {
						callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
								ErrorCodeConstants.NO_RESULT_FILL_WORD);
						return;
					}
					callback.onComplete(user);

					try {
						SharedPreferences sp = PreferenceManager
								.getDefaultSharedPreferences(mcontext);

						sp.edit()
								.putBoolean(
										SharePreferencesConstants.PREF_LOGIN_STATE,
										true)
								.putString(
										SharePreferencesConstants.PREF_USER_NAME,
										user.getUsername())
								.putString(
										SharePreferencesConstants.PREF_USER_ICON,
										user.getPhoto())
								.putString(
										SharePreferencesConstants.PREF_USER_ACCOUNT,
										username)
								.putString(
										SharePreferencesConstants.PREF_LOGIN_TYPE,
										"mobile")
								.putString(
										SharePreferencesConstants.PREF_USER_PASSWORD,
										DesEncryption.encryptDES(password,
												"20140905")).commit();

					} catch (GeneralSecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return;
					}

				}

			};
		});

	}

	public void getMyProductPage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/myclass.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("pageno", pageno);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getMyProductPage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();

					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "MyProduct");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	/**
	 * @param pageno
	 * @param callback
	 * 
	 *            返回的Object 类型是
	 *            MyProduct，其中orderId是订单id，productName是产品名，perprice是单价
	 */
	public void getUncommentPage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/myclass.do?ajax=ajax&payed=1&nocommened=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("pageno", pageno);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getUncommentPage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();

					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "MyProduct");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	/**
	 * @param targetId
	 *            课程id
	 * @param cont
	 *            评论内容
	 * @param pointDescriptionMatch
	 *            服务与描述相符度 int1~10
	 * @param pointPerfational
	 *            专业程度 int1~10
	 * @param pointServiceAttitude
	 *            服务态度 int1~10
	 * @param pointAppointmentSuccessful
	 *            预约成功率 int1~10
	 * @param callback
	 * 
	 *            因为测不了这个接口，不知道哪个是用来评分的，就先全写上了
	 */
	public void comment(final int targetId, final String cont,
			final int pointDescriptionMatch, final int pointPerfational,
			final int pointServiceAttitude,
			final int pointAppointmentSuccessful,
			final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonComment/savecomment.do?ajax=ajax&targettype=20";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("targetId", targetId);
		params.put("sessionId", cont);
		params.put("targetId", pointDescriptionMatch);
		params.put("sessionId", pointPerfational);
		params.put("targetId", pointServiceAttitude);
		params.put("sessionId", pointAppointmentSuccessful);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						comment(targetId, cont, pointDescriptionMatch,
								pointPerfational, pointServiceAttitude,
								pointAppointmentSuccessful, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});
	}

	private void getOrderDetailPage(final int pageno, final int orderid,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/userorderdetail.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("pageno", pageno);
		params.put("orderid", orderid);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
				}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getOrderDetailPage(pageno, orderid, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "MyProduct");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	public void getALLOrderPage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getALLOrderPage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}

				new OrderProduct().getOrderProductPage(page, callback);

			}

		});
	}

	class OrderProduct {
		private int pageSize;

		private boolean isFailed;

		private void getOrderProductPage(Page page,
				final GetObjectCallback callback) {
			// TODO Auto-generated method stub
			Page orderProductPage = page;
			List<Order> orderProductList = new ArrayList<Order>();
			int orderTotalSize = page.getList().size();
			pageSize = 0;

			isFailed = false;

			getDetailPage(0, orderProductPage, orderProductList,
					orderTotalSize, callback);

		}

		private void getDetailPage(final int orderIndex,
				final Page orderProductPage,
				final List<Order> orderProductList, final int orderTotalSize,
				final GetObjectCallback callback) {
			final Order order = (Order) orderProductPage.getList().get(
					orderIndex);
			final int id = order.getId();
			if (order.getStatus() == 0) {
				if (orderIndex != orderTotalSize - 1) {
					getDetailPage(orderIndex + 1, orderProductPage,
							orderProductList, orderTotalSize, callback);
				} else {
					Collections.sort(orderProductList, new ComparatorUser());
					orderProductPage.setList(orderProductList);
					orderProductPage.setPageSize(pageSize);
					callback.onComplete(orderProductPage);
				}

			} else {
				getOrderDetailPage(1, id, new GetObjectCallback() {
					private int orderProductSize;

					@Override
					public void onFailed(int errorcode, String errorword) {
						// TODO Auto-generated method stub
						isFailed = true;
						callback.onFailed(errorcode, errorword);
					}

					@Override
					public <T> void onComplete(T object) {
						// TODO Auto-generated method stub
						if (isFailed == true) {
							return;
						}
						orderProductSize = 1;
						Page page = (Page) object;
						final int lastPageNumber = page.getLastPageNumber();
						if (lastPageNumber > 1) {
							for (int k = 2; k <= lastPageNumber; k++) {
								getOrderDetailPage(k, id,
										new GetObjectCallback() {

											@Override
											public void onFailed(int errorcode,
													String errorword) {
												// TODO Auto-generated method
												// stub
												isFailed = true;
												callback.onFailed(errorcode,
														errorword);
											}

											@Override
											public <T> void onComplete(T object) {
												// TODO Auto-generated method
												// stub
												if (isFailed == true) {
													return;
												}
												orderProductSize++;

												Page page = (Page) object;

												for (int j = 0; j < page
														.getList().size(); j++) {
													MyProduct myproduct = (MyProduct) page
															.getList().get(j);
													Order orderWithName = new Order();
													orderWithName.setId(order
															.getId());
													orderWithName.setStatus(order
															.getStatus());
													orderWithName
															.setFinalPrice(myproduct
																	.getPrice());
													orderWithName
															.setCreatetimelong(order
																	.getCreatetimelong());
													orderWithName
															.setProductName(myproduct
																	.getProductName());
													orderProductList
															.add(orderWithName);
													pageSize++;
													if (j == page.getList()
															.size() - 1
															&& orderProductSize == lastPageNumber) {
														if (orderIndex == orderTotalSize - 1) {
															Collections
																	.sort(orderProductList,
																			new ComparatorUser());
															orderProductPage
																	.setList(orderProductList);
															orderProductPage
																	.setPageSize(pageSize);
															orderProductPage
																	.setThisPageLastElementNumber(pageSize);
															callback.onComplete(orderProductPage);
														} else {
															getDetailPage(
																	orderIndex + 1,
																	orderProductPage,
																	orderProductList,
																	orderTotalSize,
																	callback);

														}
													}
												}

											}
										});
							}
						}

						for (int j = 0; j < page.getList().size(); j++) {
							MyProduct myproduct = (MyProduct) page.getList()
									.get(j);
							Order orderWithName = new Order();
							orderWithName.setId(order.getId());
							orderWithName.setStatus(order.getStatus());
							orderWithName.setFinalPrice(myproduct.getPrice());
							orderWithName.setCreatetimelong(order
									.getCreatetimelong());
							orderWithName.setProductName(myproduct
									.getProductName());
							orderProductList.add(orderWithName);
							pageSize++;
							if (j == page.getList().size() - 1
									&& orderProductSize == lastPageNumber) {
								if (orderIndex == orderTotalSize - 1) {
									Collections.sort(orderProductList,
											new ComparatorUser());
									orderProductPage.setList(orderProductList);
									orderProductPage.setPageSize(pageSize);
									orderProductPage
											.setThisPageLastElementNumber(pageSize);
									callback.onComplete(orderProductPage);
								} else {
									getDetailPage(orderIndex + 1,
											orderProductPage, orderProductList,
											orderTotalSize, callback);

								}
							}
						}

					}
				});
			}

		}
	}

	public void getPaidOrderPage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax&payed=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getPaidOrderPage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				new OrderProduct().getOrderProductPage(page, callback);

			};
		});
	}

	public void getNotPaidOrderPage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax&waitpay=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getNotPaidOrderPage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				new OrderProduct().getOrderProductPage(page, callback);

			};
		});
	}

	public void getALLOrder(final int pageno, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getALLOrder(pageno, callback);

					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}

				new OrderFactory().getOrderPage(page, callback);

			}

		});
	}

	public void getPaidOrder(final int pageno, final GetObjectCallback callback) {
		
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax&payed=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getPaidOrder(pageno, callback);

					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				new OrderFactory().getOrderPage(page, callback);

			};
		});
	}

	public void getNotPaidOrder(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingOrder/userorders.do?ajax=ajax&waitpay=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getNotPaidOrder(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Order");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				new OrderFactory().getOrderPage(page, callback);

			};
		});
	}

	class OrderFactory {

		private boolean isFailed;

		private void getOrderPage(Page page, final GetObjectCallback callback) {
			// TODO Auto-generated method stub
			Page orderPage = page;
			List<Order> orderList = new ArrayList<Order>();
			int orderTotalSize = page.getList().size();

			isFailed = false;

			getDetailPage(0, orderPage, orderList, orderTotalSize, callback);

		}

		private void getDetailPage(final int orderIndex, final Page orderPage,
				final List<Order> orderList, final int orderTotalSize,
				final GetObjectCallback callback) {

			final Order order = (Order) orderPage.getList().get(orderIndex);

			final List<MyProduct> myProductList = new ArrayList<MyProduct>();

			if (order.getStatus() == 0) {
				orderPage.setTotalCount(orderPage.getTotalCount() - 1);
				orderPage.setThisPageLastElementNumber(orderPage
						.getThisPageLastElementNumber() - 1);
				if (orderIndex != orderTotalSize - 1) {
					getDetailPage(orderIndex + 1, orderPage, orderList,
							orderTotalSize, callback);
				} else {
					orderPage.setList(orderList);
					callback.onComplete(orderPage);
				}

			} else {

				getOrderDetailPage(1, order.getId(), new GetObjectCallback() {
					private int orderProductSize;

					@Override
					public void onFailed(int errorcode, String errorword) {
						// TODO Auto-generated method stub
						isFailed = true;
						callback.onFailed(errorcode, errorword);
					}

					@Override
					public <T> void onComplete(T object) {
						// TODO Auto-generated method stub
						if (isFailed == true) {
							return;
						}
						orderProductSize = 1;
						Page page = (Page) object;

						final int lastPageNumber = page.getLastPageNumber();
						if (lastPageNumber > 1) {
							for (int k = 2; k <= lastPageNumber; k++) {
								getOrderDetailPage(k, order.getId(),
										new GetObjectCallback() {

											@Override
											public void onFailed(int errorcode,
													String errorword) {
												// TODO Auto-generated
												// method stub
												isFailed = true;
												callback.onFailed(errorcode,
														errorword);
											}

											@Override
											public <T> void onComplete(T object) {
												// TODO Auto-generated
												// method stub
												if (isFailed == true) {
													return;
												}
												orderProductSize++;

												Page page = (Page) object;

												for (int j = 0; j < page
														.getList().size(); j++) {
													MyProduct myproduct = (MyProduct) page
															.getList().get(j);

													myProductList
															.add(myproduct);
													if (j == page.getList()
															.size() - 1
															&& orderProductSize == lastPageNumber) {
														order.setMyProductList(myProductList);
														orderList.add(order);
														if (orderIndex == orderTotalSize - 1) {

															orderPage
																	.setList(orderList);
															callback.onComplete(orderPage);
														} else {

															getDetailPage(
																	orderIndex + 1,
																	orderPage,
																	orderList,
																	orderTotalSize,
																	callback);

														}
													}
												}

											}
										});
							}
						}

						for (int j = 0; j < page.getList().size(); j++) {
							MyProduct myproduct = (MyProduct) page.getList()
									.get(j);

							myProductList.add(myproduct);

							if (j == page.getList().size() - 1
									&& orderProductSize == lastPageNumber) {
								order.setMyProductList(myProductList);
								orderList.add(order);
								if (orderIndex == orderTotalSize - 1) {
									orderPage.setList(orderList);
									callback.onComplete(orderPage);
								} else {

									getDetailPage(orderIndex + 1, orderPage,
											orderList, orderTotalSize, callback);

								}
							}
						}

					}
				});
			}

		}
	}

	public void getTransactionRecord(final int pageno, final int id,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/listAjax.do?sort=id desc";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);
		params.put("productid", id);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(arg0, "TransactionRecord");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	public void getMobileValidnum(final String mobilenum,
			final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL + "kailian/CmsUserInfo/mobileValidnum.do";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("mobile", mobilenum);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void register(final String mobile, final String mobilevalidnum,
			final String password, final String repassword,
			final String username, final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/regist.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("mobile", mobile);
		params.put("mobilevalidnum", mobilevalidnum);
		params.put("passwd", password);
		params.put("repasswd", repassword);
		params.put("username", username);
		params.put("thirdType", "mobile");

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void resetPassword(final String mobile, final String mobilevalidnum,
			final String password, final GetResultCallback callback) {
		
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/mobpasswd.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("mobile", mobile);
		params.put("mobilevalidnum", mobilevalidnum);
		params.put("passwd", password);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void logout(final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/logout.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				} else {
					deleteUserProperties();
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						logout(callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void loginBySocial(final String thirdType, final String openid,
			final String token, final String username, final int sex,
			final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/third.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("thirdType", thirdType);
		params.put("openid", openid);
		params.put("token", token);
		params.put("username", username);
		params.put("sex", sex);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}



			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				} else {
					callback.onComplete();

					try {
						SharedPreferences sp = PreferenceManager
								.getDefaultSharedPreferences(mcontext);
						sp.edit()
								.putString(
										SharePreferencesConstants.PREF_USER_NAME,
										username)
								.putString(
										SharePreferencesConstants.PREF_LOGIN_TYPE,
										thirdType)
								.putString(
										SharePreferencesConstants.PREF_USER_OPENID,
										DesEncryption.encryptDES(openid,
												"20140905"))
								.putString(
										SharePreferencesConstants.PREF_USER_TOKEN,
										DesEncryption.encryptDES(token,
												"20140905"))
								.putString(
										SharePreferencesConstants.PREF_USER_GENDER,
										String.valueOf(sex)).commit();

					} catch (GeneralSecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return;
					}
				}

			};
		});
	}

	public void bindMobile(final String mobile, final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsUserInfo/bindmobile.do?ajax=ajax";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("mobile", mobile);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void addShoppingcart(final int productId, final int count,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/shopCar.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("productid", productId);
		params.put("count", count);
		params.put("sessionId", sessionId);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");
				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						addShoppingcart(productId, count, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				Page page = JsonToObject.JsonToPage(arg0, "OrderShopping");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});
	}

	public void createOrder(final int orderid, final String productId,
			final int count, final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/KlShoppingShoppingcartproduct/waitpay.do?ajax=ajax";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("orderid", orderid);
		String nameParams = "name" + productId;
		params.put(nameParams, count);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						createOrder(orderid, productId, count, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});
	}

	/**
	 * @param productId
	 *            所要查询课程的课程id
	 * @param callback
	 * 
	 * 
	 *            返回值为FavoritePage，如果所查课程已收藏，则该page中装着一个该课程的rite对象，
	 *            其中有删除收藏需要用到的id值。如果该课程未收藏，则返回的page的totalcount值为0，需判断一下。
	 */
	public void isFavorite(final int productId, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonSaved/mysavetype.do?ajax=ajax&type=20";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("targetId", productId);
		params.put("sessionId", sessionId);

//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");
				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						isFavorite(productId, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Favorite");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}

				callback.onComplete(page);
			};
		});

	}

	public void getFavoritePage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonSaved/mysavetype.do?ajax=ajax&type=20";
		RequestParams params = new RequestParams(); // 绑定参数
		// params.put("ajax", "ajax");
		// params.put("type", "20");
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);
		Log.d("getFavoritePage", urlString + "&pageno=" + pageno
				+ "&sessionId=" + sessionId);
//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);
				Log.i("JsonHttpResponseHandler", "onFailure");


				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
//
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");
				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getFavoritePage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("page");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "Favorite");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);
			};
		});

	}

	/**
	 * @param targetId
	 *            是课程id productid
	 * @param callback
	 *            用来添加收藏
	 */
	public void setFavorite(final int targetId, final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonSaved/addSave.do?type=20&ajax=ajax";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("targetId", targetId);

//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();
		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						setFavorite(targetId, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				Page page = JsonToObject.JsonToPage(arg0, "SetFavorite");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);

			};
		});

	}

	/**
	 * @param id
	 *            Favorite中的id
	 * @param callback
	 *            用来删除收藏
	 */
	public void deleteFavorite(final int id, final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonSaved/deleteSave.do?ajax=ajax";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("id", id);
//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();
		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						deleteFavorite(id, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});

	}

	public void getSystemMessagePage(final int pageno,
			final GetObjectCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonMessage/messages.do?ajax=ajax&mtype=1";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);
//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();
		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");
				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getSystemMessagePage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("p1");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "KiklinkMessage");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);
			};
		});

	}

	public void getUserMessagePage(final int pageno,
			final GetObjectCallback callback) {
		
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonMessage/messages.do?ajax=ajax";
		RequestParams params = new RequestParams(); // 绑定参数
		params.put("pageno", pageno);
		params.put("sessionId", sessionId);
//		final MyHandler myHandler = new MyHandler(callback);
//		TimeThread mTimeThread = new TimeThread(myHandler);
//		Thread thread = new Thread(mTimeThread);
//		thread.start();
		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				handleHttpFailure(throwable,callback);}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
//				Message mes = myHandler.obtainMessage();
//				mes.what = 0x001;
//				myHandler.sendMessage(mes);

				Log.i("JsonHttpResponseHandler", "onSuccess ");
				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						getUserMessagePage(pageno, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				JSONObject jObject = arg0.optJSONObject("p2");
				if (jObject == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				Page page = JsonToObject.JsonToPage(jObject, "KiklinkMessage");
				if (page == null) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				if (page.getTotalCount() == 0) {
					callback.onFailed(ErrorCodeConstants.NO_RESULT_FILL,
							ErrorCodeConstants.NO_RESULT_FILL_WORD);
					return;
				}
				callback.onComplete(page);
			};
		});

	}

	/**
	 * @param id
	 *            KiklinkMessage里的id
	 * @param callback
	 */
	public void deleteMessage(final int id, final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonMessage/status.do?ajax=ajax&status=-1";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("id", id);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						deleteMessage(id, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});
	}

	public void postMessage(final int targetid, final String content,
			final GetResultCallback callback) {
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed(
					ErrorCodeConstants.NO_NETWORK_CONNECT,
					ErrorCodeConstants.NO_NETWORK_CONNECT_WORD);
			return;
		}
		String urlString = SERVER_URL
				+ "kailian/CmsCommonMessage/postmessage.do?ajax=ajax";

		RequestParams params = new RequestParams(); // 绑定参数
		params.put("sessionId", sessionId);
		params.put("targetid", targetid);
		params.put("content", content);

		HttpUtil.get(urlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				handleHttpFailure(throwable,callback);}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed(ErrorCodeConstants.NO_JSONObject_FILL,
							ErrorCodeConstants.NO_JSONObject_FILL_WORD);
					return;
				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed(ErrorCodeConstants.REQUEST_ERROR,
							arg0.optString("word"));
					return;
				}
				if (IsRelogin(arg0, new ReLoginCallback() {

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						postMessage(targetid, content, callback);
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						callback.onFailed(ErrorCodeConstants.RELOGIN_FAILED,
								ErrorCodeConstants.RELOGIN_FAILED_WORD);
						deleteUserProperties();
					}
				})) {
					return;
				}

				callback.onComplete();

			};
		});
	}

	private void Login(final ReLoginCallback callback)
			throws GeneralSecurityException {
		// TODO Auto-generated method stub

		String loginUrlString = null;
		RequestParams params = new RequestParams();

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(mcontext);
		String logintype = sp.getString(
				SharePreferencesConstants.PREF_LOGIN_TYPE, null);
		if (logintype == null) {
			callback.onFailed();
			return;
		} else if (logintype.equals("mobile")) {
			
			loginUrlString = SERVER_URL
					+ "kailian/CmsUserInfo/login.do?ajax=ajax";
			String username = sp.getString(
					SharePreferencesConstants.PREF_USER_ACCOUNT, null);

			String password = DesEncryption.decryptDES(sp.getString(
					SharePreferencesConstants.PREF_USER_PASSWORD, null),
					"20140905");
			if (username == null || password == null) {
				callback.onFailed();
				return;
			}
			params.put("sessionId", sessionId);
			params.put("moboremail", username);
			params.put("passwd", password);

		} else if (logintype.equals("weibo") || logintype.equals("qq")) {
			loginUrlString = SERVER_URL
					+ "kailian/CmsUserInfo/third.do?ajax=ajax";

			String token = DesEncryption.decryptDES(sp.getString(
					SharePreferencesConstants.PREF_USER_TOKEN, null),
					"20140905");
			String openid = DesEncryption.decryptDES(sp.getString(
					SharePreferencesConstants.PREF_USER_OPENID, null),
					"20140905");
			String username = sp.getString(
					SharePreferencesConstants.PREF_USER_NAME, null);
			String sex = sp.getString(
					SharePreferencesConstants.PREF_USER_GENDER, null);
			if (token == null || openid == null || username == null) {
				callback.onFailed();
				return;
			}
			params.put("thirdType", logintype);
			params.put("openid", openid);
			params.put("token", token);
			params.put("username", username);
			params.put("sex", sex);

		} else {
			callback.onFailed();
			return;
		}
		if(!NetWorkUtil.isNetworkConnected(mcontext))
		{
			callback.onFailed();
			return;
		}
		HttpUtil.get(loginUrlString, params, new JsonHttpResponseHandler() {

			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				Log.i("JsonHttpResponseHandler", "onFailure");
				{
					callback.onFailed();

				}
			}

			public void onSuccess(int statusCode, Header[] headers,
					JSONObject arg0) { // 返回的是JSONObject，会调用这里
				Log.i("JsonHttpResponseHandler", "onSuccess ");

				if (arg0 == null) {
					callback.onFailed();

				}
				checkSession(arg0);
				if (IsRequestFailed(arg0)) {
					callback.onFailed();

				}

				callback.onSuccess();

			};
		});

	}

	private boolean IsRelogin(JSONObject arg0, ReLoginCallback callback) {

		int re = arg0.optInt("re");
		if (re == -100) {
			try {
				Login(callback);
			} catch (GeneralSecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				callback.onFailed();
			}
			return true;
		}

		return false;
	}

	private boolean IsRequestFailed(JSONObject arg0) {

		int re = arg0.optInt("re");
		if (re == -1 || re == -2)
			return true;

		return false;
	}

	private void checkSession(JSONObject arg0) {

		String session = arg0.optString("sessionId");
		if (!session.equals(sessionId))
			sessionId = session;

	}

	public void deleteUserProperties() {

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(mcontext);
		sp.edit().putBoolean(SharePreferencesConstants.PREF_LOGIN_STATE, false)
				.remove(SharePreferencesConstants.PREF_USER_ICON)
				.remove(SharePreferencesConstants.PREF_USER_NAME)
				.remove(SharePreferencesConstants.PREF_USER_ACCOUNT)
				.remove(SharePreferencesConstants.PREF_LOGIN_TYPE)
				.remove(SharePreferencesConstants.PREF_USER_GENDER)
				.remove(SharePreferencesConstants.PREF_USER_OPENID)
				.remove(SharePreferencesConstants.PREF_USER_TOKEN)
				.remove(SharePreferencesConstants.PREF_USER_PASSWORD).commit();

	}

//	private class TimeThread implements Runnable {
//
//		private MyHandler myHandler;
//
//		public TimeThread(MyHandler myHandler) {
//			// TODO 自动生成的构造函数存根
//			this.myHandler = myHandler;
//		}
//
//		@Override
//		public void run() {
//			// TODO 自动生成的方法存根
//			int temp = 10;
//			while (temp > 0) {
//
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO 自动生成的 catch 块
//					e.printStackTrace();
//					Message mes = myHandler.obtainMessage();
//					mes.what = 0x003;
//					myHandler.sendMessage(mes);
//					return;
//				}
//
//				temp--;
//			}
//			Message mes = myHandler.obtainMessage();
//			mes.what = 0x002;
//			myHandler.sendMessage(mes);
//		}
//
//	}
//
//	@SuppressLint("HandlerLeak")
//	private class MyHandler extends Handler {
//
//		private Boolean isTimeOut;
//		private GetObjectCallback getObjectCallback;
//		private GetResultCallback getResultCallback;
//
//		public MyHandler(GetObjectCallback getObjectCallback) {
//			isTimeOut = true;
//			this.getObjectCallback = getObjectCallback;
//			this.getResultCallback = null;
//		}
//
//		public MyHandler(GetResultCallback getResultCallback) {
//			isTimeOut = true;
//			this.getObjectCallback = null;
//			this.getResultCallback = getResultCallback;
//		}
//
//		@Override
//		public void handleMessage(Message msg) {
//			// TODO 自动生成的方法存根
//			if (msg.what == 0x001) {
//				isTimeOut = false;
//			} else if (msg.what == 0x002) {
//				if (isTimeOut == true) {
//					if (getObjectCallback != null) {
//						Log.i("JsonHttpResponseHandler", "onTimeOut");
//						getObjectCallback
//								.onFailed(
//										ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
//										ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
//					}
//					if (getResultCallback != null) {
//						Log.i("JsonHttpResponseHandler", "onTimeOut");
//						getResultCallback
//								.onFailed(
//										ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
//										ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
//					}
//				}
//			} else if (msg.what == 0x003) {
//				if (getObjectCallback != null) {
//					Log.i("JsonHttpResponseHandler", "onTimeOut");
//					getObjectCallback
//							.onFailed(
//									ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
//									ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
//				}
//				if (getResultCallback != null) {
//					Log.i("JsonHttpResponseHandler", "onTimeOut");
//					getResultCallback
//							.onFailed(
//									ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE,
//									ErrorCodeConstants.JSON_HTTPRESPONSE_HANDLER_ONFAILURE_WORD);
//				}
//			}
//		}
//
//	}
}

class ComparatorUser implements Comparator<Object> {

	public int compare(Object arg0, Object arg1) {
		Order order0 = (Order) arg0;
		Order order1 = (Order) arg1;

		// 首先比较年龄，如果年龄相同，则比较名字
		if (order0.getCreatetimelong() > order1.getCreatetimelong()) {
			return -1;

		} else if (order0.getCreatetimelong() < order1.getCreatetimelong()) {
			return 1;
		} else {
			return 0;
		}

	}
}
